/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       adjusted_bitmap_cache.cpp

	$Header: /game/adjusted_bitmap_cache.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adjusted_bitmap_cache.h"

#include "bitmap_group.h"

#include "profile.h"
// -----------------------------------------------------
// bitmap cache adjusted by a value to black or white
// ---------------------------------------------------
t_adjusted_bitmap_cache_data::t_adjusted_bitmap_cache_data( t_source_cache const& source,
		                                                    int brightness )
			           : m_source( source )
{
	m_brightness = brightness;
	m_reference_count = 0;
	m_data = 0;
}

// -----------------------------------------------------
// bitmap cache adjusted by a value to black or white
// ---------------------------------------------------
t_adjusted_bitmap_cache_data::~t_adjusted_bitmap_cache_data()
{
	if (m_data != 0)
	{
		delete m_data;
		m_reference_count = 0;
	}
}
		                     
// -----------------------------------------------------
// bitmap cache adjusted by a value to black or white
// ---------------------------------------------------
void t_adjusted_bitmap_cache_data::add_reference()
{
	m_reference_count++;
}

// -----------------------------------------------------
// bitmap cache adjusted by a value to black or white
// ---------------------------------------------------
t_bitmap_group* t_adjusted_bitmap_cache_data::do_get( t_progress_handler* handler )
{
	if (m_data == 0)
	{
		t_cached_ptr<t_bitmap_group> source = m_source.get();

		if (source != 0)
		{
			declare_timer( timer, "Adjust bitmap brightness" );
			t_bitmap_group::const_iterator index = source->begin();

			m_data = new t_bitmap_group;
			m_data->reserve( source->size() );

			while (index != source->end())
			{
				m_data->push_back( (*index)->adjust_brightness( m_brightness ) );
				index++;
			}
		}
	}
	return m_data;
}

// -----------------------------------------------------
// bitmap cache adjusted by a value to black or white
// ---------------------------------------------------
void t_adjusted_bitmap_cache_data::remove_reference()
{
	if (--m_reference_count == 0)
	{
		delete m_data;
		m_data = 0;
	}
}

